﻿using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace DACU.Tools.Converters
{
	public class UriToBitmapImageConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter,
		                      CultureInfo culture)
		{
			var image = new BitmapImage();
			if (value != null)
			{
				try
				{
					image.BeginInit();
					image.CacheOption = BitmapCacheOption.OnLoad;
					image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
					image.UriSource = new Uri((string)value, UriKind.Absolute);
					image.EndInit();
				}
				catch
				{
					image = null;
				}
			}

			return image;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new Exception("The method or operation is not implemented.");
		}
	}
}